package ray.mgocc.net;

import ray.mgocc.PlayerCharacter;
import ray.mgocc.Store;

import java.io.Reader;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.File;
import java.io.Writer;
import java.io.FileWriter;
import java.io.FileReader;

import java.util.regex.*;
import java.util.List;
import java.util.ArrayList;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PlayerSelectPage{
    private final static Log log = LogFactory.getLog(PlayerSelectPage.class);
    private List<PlayerCharacter> pcs = new ArrayList<PlayerCharacter>();
    public PlayerSelectPage(){
        Reader in = null;
        try{
            in = new FileReader(getCache());
            parse(in,null);
        }catch(IOException e){
            throw new RuntimeException("cache can not use.",e);
        }finally{
            try{
                if(null!=in){
                    in.close();
                    in = null;
                }
            }catch(IOException ee){
                throw new RuntimeException("can not close file.",ee);
            }
        }
    }
    public PlayerSelectPage(Reader in){
        parse(in);
    }
    void parse(Reader in){
        Writer cache = null;
        try{
            cache = new FileWriter(getCache());
            parse(in,cache);
            cache.flush();
        }catch(IOException e){
            throw new RuntimeException("file can not use.",e);
        }finally{
            try{
                if(null!=cache){
                    cache.close();
                    cache=null;
                }
            }catch(IOException ee){
                throw new RuntimeException("file can not close.",ee);
            }
        }
    }
    void parse(Reader in,Writer cache) throws IOException{
        Pattern name = Pattern.compile(".*<span class=\"pname\">([^<+]+)</span>.*");
        Pattern id = Pattern.compile(".*;pid=([0-9]+)\".*");
        final String SEPARATOR = System.getProperty("line.separator");
        BufferedReader buf = new BufferedReader(in);
        String line;
        String pc = null;
        while(null!=(line=buf.readLine())){
            if(null!=cache){
                cache.write(line);
                cache.write(SEPARATOR);
            }
            Matcher matcher;
            matcher = name.matcher(line);
            if(matcher.matches()){
                pc = matcher.group(1);
            }
            matcher = id.matcher(line);
            if(matcher.matches()){
                if(null==pc){
                    throw new RuntimeException("parse error.");
                }
                int pid = Integer.parseInt(matcher.group(1));
                PlayerCharacter newPC = new PlayerCharacter(pid,pc);
                log.info("find pc:"+newPC);
                this.pcs.add(newPC);
                pc = null;
            }
        }
    }
    File getCache(){
        Store cache = new Store();
        return cache.getFile(PlayerSelectPage.class.getName());
    }
    public List<PlayerCharacter> getPlayerCharacters(){
        return this.pcs;
    }
}
